package com.yanqu.road.server.cross3server.manager;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.bussiness.servercenter.ChannelActivityBussiness;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.helper.SeasonHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.cross3server.conn.Cross3ConnSet;
import com.yanqu.road.utils.ConfigHelper;

import java.util.Map;

public class Cross3ChangeChannelServerMgr extends TempMgr {

    public static void cnChangeChannelServer() {

        getLogger().error("[change channel server] begin !!! ----------------");

        // 【刷新ChannelConfig，读取配置】
        getLogger().error("[change channel server] reload ChannelConfig begin...");
        try {
            int channelId = ConfigHelper.getInt("channelId");
            ChannelConfig.reloadChannelConfig(channelId);
        } catch (Exception e) {
            getLogger().error("error : ", e);
            throw new RuntimeException("[change channel server] reload ChannelConfig fail !!!");
        }

        // 【重新连接跨域】
        getLogger().error("[change channel server] reload Cross3ConnSet begin...");
        if (!Cross3ConnSet.init(0)) {
            throw new RuntimeException("[change channel server] reload Cross3ConnSet fail !!!");
        }
        getLogger().error("[change channel server] reload Cross3ConnSet success.");

        // 【获取下现在进行的活动】
        int channelId = ConfigHelper.getInt("channelId");
        String dbParam = ChannelConfig.CHANNEL_CENTER_DB_PARAM;
        Map<Integer, ActivityInfo> recentWasheInfoMap = ChannelActivityBussiness.getRecentWasheActivityInfoMap(dbParam, channelId, ChannelConfig.CHANNEL_ACTIVITY_TIME_ZONE);
        ActivityInfo info = null;
        for (ActivityInfo tmp : recentWasheInfoMap.values()) {
            info = tmp;
            break;
        }
        if (recentWasheInfoMap.isEmpty()) {
            getLogger().error("[change channel server] no washe activity !!!");
            return;
        }
        String rangeKey = SeasonHelper.getWasheRangeKey(info.getParam6());
        getLogger().error("[change channel server] washe key {} activityId {}.", rangeKey, info.getActivityId());

        getLogger().error("[change channel server] finish !!! ----------------");
    }

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        cnChangeChannelServer();
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
